package com.refuel.convert;

import cn.hutool.core.util.ObjectUtil;
import com.refuel.constant.SysConstant;
import com.refuel.domain.User;
import com.refuel.entity.UserEntity;

import java.util.Date;

/**
 * @author zhanghao
 * @description
 * @date 2024/5/24 10:43
 **/
public class UserConvert {

    /**
     * 转换为entity
     * @param user 用户对象
     * @return UserEntity
     */
    public static UserEntity toEntity(User user) {
        if (ObjectUtil.isNull(user)) {
            return null;
        }

        UserEntity userEntity = new UserEntity();
        if (ObjectUtil.isNotNull(user.getId())) {
            userEntity.setId(user.getId());
        }
        userEntity.setUid(user.getUid());
        userEntity.setIntegral(user.getIntegral());
        userEntity.setName(user.getName());
        userEntity.setHeadUrl(user.getHeadUrl());
        userEntity.setGender(user.getGender());
        userEntity.setAge(user.getAge());
        userEntity.setProvince(user.getProvince());
        userEntity.setCity(user.getCity());
        userEntity.setArea(user.getArea());
        userEntity.setLocation(user.getLocation());
        userEntity.setCreator(SysConstant.ADMIN);
        userEntity.setLastModifier(SysConstant.ADMIN);
        userEntity.setCreateTime(new Date());
        userEntity.setModifyTime(new Date());

        return userEntity;
    }

    /**
     * 转换为domain
     * @param userEntity 用户实体
     * @return User
     */
    public static User toDomain(UserEntity userEntity) {
        if (ObjectUtil.isNull(userEntity)) {
            return null;
        }

        User user = new User();
        user.setId(userEntity.getId());
        user.setUid(userEntity.getUid());
        user.setName(userEntity.getName());
        user.setIntegral(userEntity.getIntegral());
        user.setHeadUrl(userEntity.getHeadUrl());
        user.setGender(userEntity.getGender());
        user.setAge(userEntity.getAge());
        user.setLocation(userEntity.getLocation());
        user.setProvince(userEntity.getProvince());
        user.setCity(userEntity.getCity());
        user.setArea(userEntity.getArea());

        return user;
    }
}
